package com.deer.wms.base.system.service.zh.impl;

import cn.hutool.core.date.DateTime;
import com.deer.wms.base.system.dao.zh.IExceptionGroupBoxMapper;
import com.deer.wms.base.system.model.pda.Pdaweight;
import com.deer.wms.base.system.model.pda.PdaweightDto;
import com.deer.wms.base.system.model.zh.*;
import com.deer.wms.base.system.service.bill.IMaterialDetailService;
import com.deer.wms.base.system.service.zh.ElectricBoxItemService;
import com.deer.wms.base.system.service.zh.IExceptionGroupBoxService;
import com.deer.wms.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class ExceptionGroupBoxImpl implements IExceptionGroupBoxService {

    @Autowired
    private IExceptionGroupBoxMapper iExceptionGroupBoxMapper;
    @Autowired
    private ElectricBoxItemService electricBoxItemService;
    @Autowired
    private IMaterialDetailService iMaterialDetailService;

    /***
     * 请求退货
     * 修改box_item_exception表晶棒状态
     * 单晶插入box_item_exception_groupbox_detail表
     * 组箱插入box_item_exception_groupbox表
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public GroupBoxResultModel GroupBoxOut(String id, ArrayList<BarExceptionComModel> arrayList, String user_id, Double weight, String matnr) {
        GroupBoxResultModel groupBoxResultModel = new GroupBoxResultModel("NG", "组箱失败");
        try {
            if (user_id == null || user_id.equals("")) {
                return new GroupBoxResultModel("NG", "[未接收到提交的用户信息]");
            }
            if (id == null || id.equals("")) {
                return new GroupBoxResultModel("NG", "[未接收到提交的单据信息]");
            }
            if (arrayList == null) {
                return new GroupBoxResultModel("NG", "[未接收到提交的晶棒信息]");
            }
            Pdaweight pdaweight = new Pdaweight();
            pdaweight.setBill_in_sap_detail_id(Integer.parseInt(id));
            List<PdaweightDto> list = new ArrayList();
            List<PdaweightDto> electricBoxDetails = electricBoxItemService.selectUnfinished(pdaweight);
            for (PdaweightDto e : electricBoxDetails
            ) {
                Pdaweight d = iMaterialDetailService.selectWeight(e.getBill_in_sap_detail_id() + "");
                if (d == null) {
                    d = new Pdaweight();
                    d.setCompleted(0.0);
                }
                PdaweightDto p = new PdaweightDto();
                p.setBill_in_sap_detail_id(e.getBill_in_sap_detail_id());
                p.setMATNR(e.getMATNR());
                p.setVBELN(e.getVBELN());
                p.setVTEXT(e.getVTEXT());
                p.setLGMNG(e.getLGMNG());
                p.setCompleted(d.getCompleted());
                BigDecimal num1 = new BigDecimal(Double.toString(e.getLGMNG()));

                BigDecimal num2 = new BigDecimal(Double.toString(d.getCompleted()));
                BigDecimal result = num1.subtract(num2);
                p.setIncomplete(result.doubleValue());
                list.add(p);
            }
            Double incomplete = list.get(0).getIncomplete();
            if (incomplete<weight){
                return new GroupBoxResultModel("NG", "[当前组托重量已经超出单据剩余的重量]");
            }
            //#{groupbox_id},#{bill_in_sap_detail_id},#{user_id},#{Weight},GETDATE(),0,#{F_Degree},#{MATNR}
            String yyMMddHHmm = new DateTime().toString("yyMMddHHmm");
            int i = iExceptionGroupBoxMapper.insertExceptionGroupbox("TH" + yyMMddHHmm, id, user_id, weight, "PC" + yyMMddHHmm, matnr);
            if (i != 1) throw new RuntimeException();
            int j = 0;
            for (BarExceptionComModel boxid : arrayList) {
                //新增组箱退货绑定exception_id,groupbox_id,MATNR,Weight,F_Degree
                i = iExceptionGroupBoxMapper.insertExceptionGroupboxDetail(Integer.parseInt(boxid.getParentMaterialId()), "TH" + yyMMddHHmm);
                if (i != 1) {
                    groupBoxResultModel = new GroupBoxResultModel("NG", "组箱失败,第" + j + "根晶棒出现问题");
                    throw new RuntimeException();
                }
                //修改异常区状态
                i = iExceptionGroupBoxMapper.updateExceptionState(Integer.parseInt(boxid.getParentMaterialId()));
                j++;
                if (i != 1) {
                    groupBoxResultModel = new GroupBoxResultModel("NG", "组箱失败,第" + (j - 1) + "根晶棒出现问题");
                    throw new RuntimeException();
                }
                //TODO 标识此来料单明细已经组盘
            }
        } catch (RuntimeException re) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return groupBoxResultModel;
        }
        return new GroupBoxResultModel("OK", "组箱成功");
    }

    /***
     * 查询退货单据
     * 查询组箱要求
     * 剩余托数
     * @return
     */
    @Override
    public List<GroupBoxBillOutModel> findGroupBoxBillOut() {
        List<GroupBoxBillOutModel> list = iExceptionGroupBoxMapper.findGroupBoxBillOut();
        for (GroupBoxBillOutModel G : list) {
            //计算当前单据剩余托数
            //i=当前已经组托数量
            int i = iExceptionGroupBoxMapper.findGroupBox(G.getBill_in_sap_detail_id());
            i = G.getvOLUM() - i;
            G.setvOLUM(i);
        }
        return list;
    }

    /***
     * 查询晶棒信息
     * 检查当前晶棒是否已经组箱
     * 批次
     * 物料编码
     * 重量
     */
    @Override
    public GroupBoxBarElectricBillModel findBarElectricBill(String BarNumber) {
        GroupBoxBarElectricBillModel G = iExceptionGroupBoxMapper.findBarElectricBill(BarNumber);
        return G;
    }

    @Override
    public PdaSysUserModel selectUser(String login_name) {
        return iExceptionGroupBoxMapper.selectUser(login_name);
    }

    @Override
    public boolean joinPlantForBackAtPC(String materialId, String carrierId) {

//        iExceptionGroupBoxMapper.

        return false;
    }

}
